// 电影--表现层
var express = require('express');
var router = express.Router();
const {
    get,
    create,
    getById,
    update,
    removeData
} = require("../service/moviesService") //自行引对应service方法


router.get('/', async function (req, res, next) {
    const data = await get();
    // console.log(data);
    res.send(data);
});
router.post('/create', async function (req, res, next) {
    const params = req.body;
    params.director = JSON.parse(params.director);
    params.actor = JSON.parse(params.actor);
    const data = await create(params);
    res.send(data);
});

router.patch('/:_id', async function (req, res, next) {
    const {
        _id
    } = req.params;
    const params = req.body;
    if ( params.director) {
        params.director = JSON.parse(params.director);
    }
    if (params.actor) {
        params.actor = JSON.parse(params.actor);
    }
    for (const key in params) {
        if (/^\$/.test(key)) {
            params[key] = JSON.parse(params[key]);
        }
    }
    await update(_id, params);
    const result = await getById(_id);
    res.send(result);
});

router.get('/:_id', async function (req, res, next) {
    const {
        _id
    } = req.params;
    const data = await getById(_id);
    // console.log(data);
    res.send(data);
});

router.post('/delete', async function (req, res, next) {
    const {_id} = req.body;
    const result = await removeData(_id);
    res.send(result);
});

module.exports = router;